; /////////////////////////////////////////////////////////////////////////////
;
;     This file is part of PurePicBasic.
; 
;     PurePicBasic is free software: you can redistribute it And/Or modify
;     it under the terms of the GNU General Public License As published by
;     the Free Software Foundation, either version 3 of the License, Or
;     (at your option) any later version.
; 
;     PurePicBasic is distributed in the hope that it will be useful,
;     but WITHOUT ANY WARRANTY; without even the implied warranty of
;     MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE.  See the
;     GNU General Public License For more details.
; 
;     You should have received a copy of the GNU General Public License
;     along With PurePicBasic.  If Not, see <http://www.gnu.org/licenses/>.
;
; /////////////////////////////////////////////////////////////////////////////

IncludePath  "..\include\"
XIncludeFile "structures.pbi"
XIncludeFile "errors.pbi"
XIncludeFile "lexer.pbi"
XIncludeFile "parser.pbi"
XIncludeFile "symbol_table.pbi"
XIncludeFile "def_file.pbi"
XIncludeFile "intermediate.pbi"


Procedure DispHelp()
  PrintN("-------------------------------------------------------------------------------")
  PrintN("PurePicBasicCompiler by J.Ortali & D.Poulin, 2013")
  PrintN("-------------------------------------------------------------------------------")
  PrintN(" Usage :   -source <filename> : Compile file")
  PrintN("           -help : Display this help")
  PrintN("           -standalone : Compiler use standalone" )
  PrintN("           -dump : Dump Symbole Table")
  PrintN("-------------------------------------------------------------------------------")
EndProcedure

If OpenConsole("Pure Pic Basic Compiler")
  
    argc = CountProgramParameters()
  
  If argc > 0
    
    For i = 0 To argc - 1
      
      Select ProgramParameter(i)
          
        Case "-help"
          DispHelp()
        Case "-standalone"
          argStandAlone = #True
        Case "-chipset"
          currentChipset = ProgramParameter(i+1)
        Case "-source"
          source_file\filename = ProgramParameter(i+1)
        Case "-dump"
          argDumpSymboleTable = #True
        Case "-verbose"
          argVerbose = #True    
      EndSelect
      
    Next
    
  Else
    DispHelp()
  EndIf
  
  
  If FileSize(source_file\filename) > 0 
    
    ;PrintN("Chipset : " + currentChipset)
    
    _read_def_file_(currentChipset)
    
    _init_map_types_()
    
    _init_map_mots_reserve_()
    
    _open_source_file_(@source_file)
    
    _clean_source_(@source_file)
    
    _tokenize_file_(@source_file)
    
    _evaluate_tokens_(@source_file)
    
    _parse_(@source_file)       
    
    If argDumpSymboleTable And argVerbose 
      _dump_symbol_table_()
    EndIf
    
    _write_tac_()
    
    ;!!!!!!!!!!!!!!!!!!!!!! Test
    
    Debug "----------------------------------------------------------"
    
    ForEach T_Label()
      Debug T_Label()\name + " -> " +T_Label()\labelName + "  imb: " + Str(T_Label()\imbrication) + "  adr_ref: " + Str(T_Label()\adr_ref) + "  adr_reel: " + Str(T_Label()\adr_reel)
    Next
    
  Else
    PrintN("Impossible d'ouvrir le fichier source")
    argDumpSymboleTable = #False
    Exit()
  EndIf
  
  If argStandAlone
    Repeat : Until Inkey() <> ""
  EndIf
  
  CloseConsole()
EndIf
; IDE Options = PureBasic 5.10 (Windows - x86)
; CursorPosition = 102
; FirstLine = 69
; Folding = -
; EnableXP